package com.ruifu.proj.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.eneity.BaseEntityPlus;
import com.ruifu.common.po.Toolbar;
import com.ruifu.common.utils.BeanConverter;
import com.ruifu.proj.conf.CustomToolbar;
import com.ruifu.proj.mapper.ToolbarMapper;
import com.ruifu.proj.service.ToolbarService;
import com.ruifu.proj.vo.toolbar.ToolbarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nyr
 * @create 2019/11/25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ToolbarServiceImpl extends ServiceImpl<ToolbarMapper, Toolbar> implements ToolbarService {
    @Autowired(required = false)
    private ToolbarMapper toolbarMapper;

    @Autowired
    private CustomToolbar customToolbar;

    /**
     * 查询可以再添加的菜单
     *
     * @param tlObjType 需要添加的菜单类型
     * @return
     */
    @Override
    public R findList(String tlObjType) {
        List<Toolbar> selectToolbars = toolbarMapper.selectList(Wrappers.<Toolbar>query()
                .lambda()
                .eq(Toolbar::getTlObjType, tlObjType)
                .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()));
        List<ToolbarVO> sourceToolbars = customToolbar.getToolbars()
                .stream()
                .filter(toolbarVO -> tlObjType.equals(toolbarVO.getTlObjType()))
                .collect(Collectors.toList());
        List<ToolbarVO> targetToolbars = BeanConverter.copyList(sourceToolbars, ToolbarVO.class);
        sourceToolbars.forEach(sourceToolbar -> selectToolbars.forEach(selectToolbar -> {
            if (sourceToolbar.getTlPermissionCode().equals(selectToolbar.getTlPermissionCode())) {
                targetToolbars.remove(sourceToolbar);
            }
        }));
        return R.ok(targetToolbars);
    }
}